﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Configuration;

public partial class GroupBuying_EditBuy : BasePage
{
    Access access = new Access();
    GroupBuyingBO gbo = new GroupBuyingBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (GetQeuryString("para") == "update")
            {
                InitData();
            }
            else if (GetQeuryString("para") == "add")
            {
                //this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (GetQeuryString("para") == "update")
            {
                Model.GroupBuying menu = GetEntity();
                menu.ID = Convert.ToInt32(GetQeuryString("id"));
                gbo.Update(menu);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            }
            else if (GetQeuryString("para") == "add")
            {
                Model.GroupBuying m = GetEntity();
                if (m.Status==1&&gbo.GetModel(CurrentUserInfo.MID,0) != null)
                {
                    AlertMessage("启用状态的团购只能有一个");
                    return;
                }
                gbo.Add(m);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            }
        }
        catch (Exception)
        {
            AlertMessage("请将信息填写完整");
        }
    }

    private void InitData()
    {
        string id = GetQeuryString("id");
        GroupBuying ds = gbo.GetModel(int.Parse(id));
        txtDescribe.Text = ds.Describe;
        txtEndTime.Text = ds.EndTime.ToString();
        txtKeyWords.Text = ds.KeyWords;
        txtNewPrice.Text = ds.NewPrice.ToString();
        txtOldPrice.Text = ds.OldPrice.ToString();
        txtPicUrl.Text = ds.PicUrl;
        txtPlaces.Text = ds.Places.ToString();
        txtRemind.Text = ds.Remind;
        txtStartTime.Text = ds.StartTime.ToString();
        txtSummary.Text = ds.Summary;
        txtTitle.Text = ds.Title;
        Image1.ImageUrl = ds.PicUrl;
        txtGBUrl.Text = ds.GBUrl;
        rbtA.Checked = ds.Status == 1 ? true : false;
        rbtB.Checked = ds.Status == 2 ? true : false;
        txtMaxNum.Text = ds.MaxNum.ToString();
    }
    public Model.GroupBuying GetEntity()
    {
        Model.GroupBuying m = new Model.GroupBuying();
        if (GetQeuryString("para") == "update")
        {
            m = gbo.GetModel(Convert.ToInt32(GetQeuryString("id")));
        }
        else
        {
            m.Purchases = 0;
            m.VisitorVolume = 0;
        }
        m.Describe = txtDescribe.Text;
        m.EndTime = DateTime.Parse(txtEndTime.Text);
        m.KeyWords = txtKeyWords.Text;
        m.NewPrice = decimal.Parse(txtNewPrice.Text);
        m.OldPrice = decimal.Parse(txtOldPrice.Text);
        m.PicUrl = txtPicUrl.Text;
        m.Places = int.Parse(txtPlaces.Text);
        m.Remind = txtRemind.Text;
        m.StartTime = DateTime.Parse(txtStartTime.Text);
        m.Summary = txtSummary.Text;
        m.Title = txtTitle.Text;
        m.GBUrl = txtGBUrl.Text;
        m.Status = rbtA.Checked ? 1 : 2;
        m.MID = CurrentUserInfo.MID;
        try
        {
            m.MaxNum = int.Parse(txtMaxNum.Text);
        }
        catch (Exception)
        {
            m.MaxNum = 1;
        }

        return m;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                //else if (fileSize > 52100)
                //{
                //    AlertMessage("请上传50K以内的JPG格式照片");
                //}
                else
                {
                    //if (File.Exists(path + CurrentUserInfo.ID + ".jpg"))
                    //{
                    //    File.Delete(path + CurrentUserInfo.ID + ".jpg");
                    //}
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image1.ImageUrl = "Images/" + guid + ".jpg";
                            txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] + "/GroupBuying/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
        //}
    }
}